package coffeebar;

import java.awt.*;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;

public class TransactionControls extends JPanel {
	JPanel transactionArea;
	JPanel buttonArea;

	// JList transactions;
	JTable transactions;
	JScrollPane transactionsScroll;
	// JLabel total;

	JButton confirm;
	JButton cancel;
	JButton free;

	static Object[] currentData;

	public void update() {
		Transaction t = Controller.getCurrentTransaction();
		refreshTable();
		// currentData = t.getTransactions();
		// transactions.updateUI();
		// transactions.setSelectedIndex(t.getTransactions().length - 1);
		// transactions.ensureIndexIsVisible(t.getTransactions().length - 1);
		confirm.setEnabled(!t.isEmpty());
		cancel.setEnabled(!t.isEmpty());
		System.out.println("updating table");
	}

	public TransactionControls() {
		transactionArea = new JPanel();
		transactionArea.setLayout(new BorderLayout());

		// total = new JLabel("Total: 0 $");
		// transactions = new JTable(Controller.getTransactionArray(),new
		transactions = new JTable(new MyTableModel());
		transactions.setBackground(Color.white);
		transactions.setGridColor(Color.white);
		
			//TableColumn col = transactions.getColumnModel().getColumn(0);
			//col.setCellRenderer(new MyTableCellRenderer());
			//col = transactions.getColumnModel().getColumn(1);
			//col.setCellRenderer(new MyTableCellRenderer());
			
	   transactions.setDefaultRenderer(Object.class,  new MyTableCellRenderer() );
		
		
		transactionsScroll = new JScrollPane(transactions) {
			public Dimension getPreferredSize() {
				Dimension d = super.getPreferredSize();
				d.height = 100;
				return d;
			}
		};

		transactionArea.add(transactionsScroll, BorderLayout.CENTER);

		buttonArea = new JPanel();
		buttonArea.setLayout(new BorderLayout());

		confirm = new JButton("Confirm");
		cancel = new JButton("Clear");
		free = new JButton("Discount");

		confirm.addActionListener(new ButtonActionListener());
		cancel.addActionListener(new ButtonActionListener());
		free.addActionListener(new ButtonActionListener());

		buttonArea.add(free, BorderLayout.NORTH);
		buttonArea.add(confirm, BorderLayout.WEST);
		buttonArea.add(cancel, BorderLayout.EAST);

		this.setLayout(new BorderLayout());
		this.add(transactionArea, BorderLayout.CENTER);
		this.add(buttonArea, BorderLayout.EAST);

	}

	class ButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JButton button = (JButton) e.getSource();
			if (button == cancel) {
				Controller.cancelTransaction();
				Controller.getGUI().showItems();
				Controller.gui.update();
			} else if (button == confirm) {
				submitTransaction();
			} else if (button == free) {
				if (Controller.getGUI().items.isVisible())
					Controller.getGUI().showUsers();
				else
					Controller.getGUI().showItems();
			}
		}
	}

	public void submitTransaction() {
		Controller.submitTransaction();
		Controller.gui.update();
	}
	
	public void refreshTable() {
		// transactions.getModel().addRow(it.toArray());
		((MyTableModel) transactions.getModel()).refresh();
		transactions.setRowSelectionInterval(transactions.getRowCount()-1, transactions.getRowCount()-1);
	}
}

class MyTableModel extends AbstractTableModel {

	private Object[][] data = Controller.getTransactionArray();

	public int getColumnCount() {
		return 3;
	}

	public int getRowCount() {
		if (data != null) {
			return data.length;
		} else
			return 0;
	}

	public String getColumnName(int col) {
		return "";
	}

	public void refresh() {
		data = Controller.getTransactionArray();
		fireTableDataChanged();
	}

	public Object getValueAt(int row, int col) {
		return data[row][col];
	}

	/*
	 * Don't need to implement this method unless your table's editable.
	 */
	// public boolean isCellEditable(int row, int col) {
	// //Note that the data/cell address is constant,
	// //no matter where the cell appears onscreen.
	// if (col < 2) {
	// return false;
	// } else {
	// return true;
	// }
	// }
	/*
	 * Don't need to implement this method unless your table's data can change.
	 */
	public void setValueAt(Object value, int row, int col) {
		data[row][col] = value;
		fireTableCellUpdated(row, col);
	}

	
}

class MyTableCellRenderer
extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent
    (JTable table, Object value, boolean selected, boolean focused, int row, int column)
{
    setEnabled(table == null || table.isEnabled()); // see question above

    if (row  == table.getRowCount() - 1) {
        setBackground(table.getSelectionBackground());
    	if (column == 2) { //FIXME remove this 3 magic number, 3 is the number of cols
    		value = "";
    	}
	}
    else {
    	if (column == 1) {
    		//add button:
    		
    	}
        setBackground(null);
    }

    
    super.getTableCellRendererComponent(table, value, selected, focused, row, column);

    return this;
}
}
